package com.yan.proxy.dynamic.javaassist;

import com.yan.proxy.IGamePlayer;
import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.Proxy;
import javassist.util.proxy.ProxyFactory;

public class GamePlayerProxy<T extends IGamePlayer> {

    @SuppressWarnings("all")
    public <E extends T> E getProxy(Class<E> clazz) throws InstantiationException, IllegalAccessException {
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setSuperclass(clazz);
        Class<?> aClass = proxyFactory.createClass();
        E o = (E) aClass.newInstance();
        MethodHandler mi = (self, m, proceed, args) -> {
            System.out.println("Name: " + m.getName());
            return proceed.invoke(self, args);  // execute the original method.
        };
        ((Proxy) o).setHandler(mi);
        return o;
    }

}
